package com.jasonjan.jiance.binder;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jakewharton.rxbinding2.view.RxView;
import com.jasonjan.jiance.R;
import com.jasonjan.jiance.bean.HomeNews;
import com.jasonjan.jiance.global.ErrorAction;
import com.jasonjan.jiance.module.home.content.HomeWebView;

import java.util.concurrent.TimeUnit;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import me.drakeet.multitype.ItemViewBinder;

/**
 * Created by jasonjan on 2018/3/13.
 */

public class HomeNewsBinder extends ItemViewBinder<HomeNews.ResultBean.DataBean,HomeNewsBinder.ViewHolder> {

    @NonNull
    @Override
    protected HomeNewsBinder.ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent){
        View view = inflater.inflate(R.layout.item_home_title, parent, false);
        return new ViewHolder(view);
    }


    @Override
    protected void onBindViewHolder(@NonNull final ViewHolder holder, @NonNull final HomeNews.ResultBean.DataBean item){
        final Context context=holder.itemView.getContext();
        try{
            Log.d("\n主页item:",""+item.toString());
            if(item.getType().equals("NEW")){
                holder.school_list_status.setImageResource(R.mipmap.school_new);
                holder.school_list_status.setVisibility(View.VISIBLE);
            }else if(item.getType().equals("HOT")){
                holder.school_list_status.setImageResource(R.mipmap.school_hot);
                holder.school_list_status.setVisibility(View.VISIBLE);
            }else{
                holder.school_list_status.setVisibility(View.GONE);
            }

            holder.school_list_content.setText(item.getTitle());
            if(item.getTitle().length()>14){
                holder.school_list_content.setText(item.getTitle().substring(0,14)+"...");
            }

            RxView.clicks(holder.itemView)
                    .throttleFirst(1, TimeUnit.SECONDS)
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(@NonNull Object o) throws Exception {
                            /*PhotoContentActivity.launch(item);*/
                            HomeWebView.Companion.launch(item);
                        }
                    });
        }catch(Exception e){
            ErrorAction.print(e);
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView school_list_status;
        private TextView school_list_content;


        public ViewHolder(View itemView) {
            super(itemView);
            this.school_list_content = itemView.findViewById(R.id.school_list_content);
            this.school_list_status = itemView.findViewById(R.id.school_list_status);

        }
    }
}
